package com.chb.service.impl;

import com.chb.dao.GoodsDao;
import com.chb.dao.InventoryDao;
import com.chb.dao.MainDao;
import com.chb.model.entity.Staff;
import com.chb.model.vo.*;
import com.chb.service.InventoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ：njh
 * @date ：Created in 2023/10/10 11:01
 * @description：盘点业务层实现类
 * @modified By：
 * @version:
 */
@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private MainDao mainDao;
    @Autowired
    private HttpSession session;


    @Override
    public Result checkState() {
        //获取员工id,分店id
        Staff staff = (Staff) session.getAttribute("staff");

        System.out.println(staff.getBranchid());

        Integer count = inventoryDao.checkState(staff.getBranchid());

        if (count != 0) {
            return Result.error();
        } else {
            return Result.success();
        }
    }


    @Override
    @Transactional
    public Result addInventory(List<InventoryVo> inventoryVoList) {

        //获取员工id,分店id
        Staff staff = (Staff) session.getAttribute("staff");

        //添加盘点表信息(inventory)
        Integer row = inventoryDao.addInventory(staff.getStaffid(), staff.getBranchid());

        //获取最后一次自增的id
        Integer i = inventoryDao.lastInsertId();

        //数组里添加获取的盘点id
        for (InventoryVo o :
                inventoryVoList) {
            o.setInvenid(i);
        }
        //添加盘点商品详细表信息(inventory_goods)
        Integer row2 = inventoryDao.addInventoryGoods(inventoryVoList);

        return Result.success();

    }

    @Override
    public Result findAllInventory(Integer currentPage, Integer pageSize) {

        PageHelper.startPage(currentPage, pageSize);
        List<InventoryDataVo> list = inventoryDao.findAllInventory();
        PageInfo<InventoryDataVo> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }

    @Override
    public List<InventoryDataVo> findAllInventory(Integer state) {
        List<InventoryDataVo> allInventory = inventoryDao.findAllInventory(state);
        return allInventory;
    }

    @Override
    public Result findAllInventory(Integer currentPage, Integer pageSize, String startTime, String endTime, Integer branchid, Integer state) {

        PageHelper.startPage(currentPage, pageSize);
        List<InventoryDataVo> list = inventoryDao.findAllInventory(startTime, endTime, branchid, state);
        PageInfo<InventoryDataVo> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }

    @Override
    public Result searchInventoryByBranchId(Integer currentPage, Integer pageSize, String startTime, String endTime, Integer branchid, Integer state) {

        PageHelper.startPage(currentPage, pageSize);
        List<InventoryDataVo> list = inventoryDao.searchInventoryByBranchId(startTime, endTime, branchid, state);
        PageInfo<InventoryDataVo> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }


    @Override
    public Result findInventoryGoodsByInventoryID(Integer currentPage, Integer pageSize, Integer invenid) {

        PageHelper.startPage(currentPage, pageSize);
        List<InventoryGoodsVo> list = inventoryDao.findInventoryGoodsByInventoryID(invenid);
        PageInfo<InventoryGoodsVo> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }

    @Override
    @Transactional
    public Integer addReject(InventoryDataVo inventoryDataVo) {

        //获取员工id,分店id
        Staff staff = (Staff) session.getAttribute("staff");

        //添加审核员的id
        inventoryDataVo.setExamstaffid(staff.getStaffid());

        Integer row = inventoryDao.addReject(inventoryDataVo);
        return row;

    }

    @Override
    @Transactional
    public Integer addPass(InventoryDataVo inventoryDataVo) {

        //获取员工id,分店id
        Staff staff = (Staff) session.getAttribute("staff");

        //添加审核员的id
        inventoryDataVo.setExamstaffid(staff.getStaffid());

        Integer row = inventoryDao.addPass(inventoryDataVo);
        return row;
    }

    @Override
    @Transactional
    public Integer updPassCount(List<InventoryGoodsVo> inventoryGoodsVo) {

        for (InventoryGoodsVo igv :
                inventoryGoodsVo) {
            //获取报损数量
            Integer lossnum = igv.getLossnum();

            //如果有报损，则修改商品数量
            if (lossnum > 0) {
                //修改
                inventoryDao.updPassCount(igv);
            }
        }

        return 0;

    }


}